﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using AccesoDatos;

namespace Devoluciones
{

    public partial class frmCapturaDev : Form
    {
        [DllImport("user32.dll")]
        static extern bool AnimateWindow(IntPtr hwnd, uint dwTime, uint dwFlags);

        public class WinApi
        {
            public static int GWL_EXSTYLE = -20;
            public static int WS_EX_LAYERED = 0x80000;
            public static int LWA_ALPHA = 0x2;
            [DllImport("user32")]
            public static extern int SetLayeredWindowAttributes(IntPtr hWnd, byte crey, byte alpha, int dwFlags);
            [DllImport("user32")]
            public static extern int GetLayeredWindowAttributes(IntPtr hWnd, byte crey, out byte alpha, int dwFlags);
            [DllImport("user32")]
            public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
            [DllImport("user32")]
            public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        }
        void mkwndtrans(IntPtr hWnd, int num)
        {
            int oldWinStyle = WinApi.GetWindowLong(hWnd, WinApi.GWL_EXSTYLE);
            WinApi.SetWindowLong(hWnd, WinApi.GWL_EXSTYLE,
            num < 255 ? oldWinStyle | WinApi.WS_EX_LAYERED :
            oldWinStyle & WinApi.WS_EX_LAYERED);
            WinApi.SetLayeredWindowAttributes(hWnd, 0, (byte)num, WinApi.LWA_ALPHA);
        }
        enum AnimateWindowFlags : uint
        {
            AW_HOR_POSITIVE = 0x00000007,
            AW_HOR_NEGATIVE = 0x00000002,
            AW_VER_POSITIVE = 0x00000004,
            AW_VER_NEGATIVE = 0x00000008,
            AW_CENTER = 0x00000010,
            AW_HIDE = 0x00010000,
            AW_ACTIVATE = 0x00020000,
            AW_SLIDE = 0x00040000,
            AW_BLEND = 0x00080000
        }

        List<clsDevoluciones> lstDevolucion = new List<clsDevoluciones>();
        List<clsDevoluciones> lstDevCliente = new List<clsDevoluciones>();

        public frmCapturaDev()
        {
            InitializeComponent();
        }
        private void frmCapturaDev_Load(object sender, EventArgs e)
        {
            txtCliente.Focus();
        }
        private void txtCliente_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                frmListFacturas frmF = new frmListFacturas(txtCliente.Text);
                AnimateWindow(frmF.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
                frmF.ShowDialog();

                if (frmF.StrFactura != "")
                {
                    CargaArticulosDev(frmF.StrFactura);
                }
                if (frmF.Nombre != "")
                {
                    lblNombreCliente.Text = frmF.Nombre;
                }
            }
        }

        
        public void CargaArticulosDev(string strFolioFact)
        {
            clsDevoluciones objDev = new clsDevoluciones();
            List<clsDevoluciones> lstArtDev = new List<clsDevoluciones>();
            try
            {
                lstArtDev = objDev.RegresaProductosFactura(strFolioFact);
                dgvArticulosDev.AutoGenerateColumns = false;
                dgvArticulosDev.DataSource = lstArtDev;
                lstDevolucion = lstArtDev;
                foreach (DataGridViewColumn c in dgvArticulosDev.Columns)
                {
                    c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dgvArticulosDev_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                clsDevoluciones objDev = new clsDevoluciones();
                int Cant = int.Parse(dgvArticulosDev.CurrentRow.Cells[1].Value.ToString());
                string Clave = dgvArticulosDev.CurrentRow.Cells[0].Value.ToString();
                decimal Precio = decimal.Parse(dgvArticulosDev.CurrentRow.Cells[3].Value.ToString().Replace("$","").Replace(",",""));
                frmCantDev frmC = new frmCantDev(Clave, Cant, Precio);
                AnimateWindow(frmC.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
                frmC.ShowDialog();

                if (frmC.Cantidad != 0 && frmC.Producto != "")
                {
                    objDev = new clsDevoluciones();
                    objDev.Cantidad = frmC.Cantidad;
                    objDev.Clave = frmC.Producto;
                    objDev.Precio = frmC.Precio;
                    lstDevCliente.Add(objDev);
                    dgvArtDevolver.AutoGenerateColumns = false;
                    dgvArtDevolver.DataSource = null;
                    dgvArtDevolver.DataSource = lstDevCliente;
                    foreach (DataGridViewColumn c in dgvArtDevolver.Columns)
                    {
                        c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    }

                    //RESTA CANTIDAD
                    ProcesaArtDevolucion(lstDevolucion, frmC.Cantidad, frmC.Producto);

                }

            }
        }
        public void ProcesaArtDevolucion(List<clsDevoluciones> lstDev, int intCantidad, string strClave)
        {
            clsDevoluciones objDev = new clsDevoluciones();
            lstDev.ForEach(delegate(clsDevoluciones objD)
            {
                objDev = new clsDevoluciones();
                objDev.Clave = strClave;
                objDev.Cantidad = intCantidad;
                lstDev = lstDev.FindAll(objDev.ProcesaCantidad);
            });

            dgvArticulosDev.AutoGenerateColumns = false;
            dgvArticulosDev.DataSource = lstDev;
            lstDevolucion = lstDev;
        }
        

       
    }
}
